package cn.knet.sp.ncrm.modules.mywork.potentialcustomer.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import cn.knet.sp.ncrm.common.service.BaseService;
import cn.knet.sp.ncrm.modules.customer.aftersalesservice.entity.PotentialAfterSalesService;
import cn.knet.sp.ncrm.modules.customer.aftersalesservice.service.PotentialAfterSalesServiceService;
import cn.knet.sp.ncrm.modules.customer.appointment.entity.PotentialAppointmentInfo;
import cn.knet.sp.ncrm.modules.customer.appointment.service.PotentialAppointmentInfoService;
import cn.knet.sp.ncrm.modules.customer.importsupport.ImportService;
import cn.knet.sp.ncrm.modules.customer.potentialadmingroup.entity.PotentialAdminGroup;
import cn.knet.sp.ncrm.modules.customer.potentialadmingroup.service.PotentialAdminGroupService;
import cn.knet.sp.ncrm.modules.customer.potentialcustomer.dao.PotentialCustomerDao;
import cn.knet.sp.ncrm.modules.customer.potentialcustomer.dao.PotentialCustomerStatusDao;
import cn.knet.sp.ncrm.modules.customer.potentialcustomer.entity.PotentialCustomer;
import cn.knet.sp.ncrm.modules.customer.potentialcustomer.entity.PotentialCustomerStatus;
import cn.knet.sp.ncrm.modules.customer.potentialcustomer.service.PotentialCustomerService;
import cn.knet.sp.ncrm.modules.customer.potentialcustomer.support.DistEnum;
import cn.knet.sp.ncrm.modules.customer.visitrecord.entity.VisitRecord;
import cn.knet.sp.ncrm.modules.customer.visitrecord.service.VisitRecordService;
import cn.knet.sp.ncrm.modules.sys.customerstatus.entity.CustomerStatus;
import cn.knet.sp.ncrm.modules.sys.customerstatus.service.CustomerStatusService;

/**
 * TODO(这个类的作用)My potential customer service.
 * @author fengyapeng
 * @since 2015 /09/10 18:39:32
 * @INFO:
 * @Author: Feng Yapeng
 * @Time: 2015 /9/9 13:51
 */
@Service
public class MyPotentialCustomerService extends BaseService {

	@Autowired
	private PotentialCustomerService potentialCustomerService;
	@Resource
	private VisitRecordService       visitRecordService;

	@Resource
	private PotentialAppointmentInfoService potentialAppointmentInfoService;

	@Resource
	private PotentialAfterSalesServiceService potentialAfterSalesServiceService;


	@Autowired
	private PotentialAdminGroupService potentialAdminGroupService;

	@Resource
	private ImportService importService;

	@Resource
	private CustomerStatusService customerStatusService;

	@Resource
	private PotentialCustomerDao potentialCustomerDao;
	@Resource
	private PotentialCustomerStatusDao potentialCustomerStatusDao;

	@Transactional(readOnly = false)
	public void visitRecordCreate(PotentialCustomer potentialCustomer, VisitRecord myVisitRecord,
								  PotentialAppointmentInfo potentialAppointmentInfo) {
		visitRecordService.createVisitRecord(potentialCustomer, myVisitRecord);
		this.potentialAppointmentInfoService.createAppointmentInfo(potentialCustomer, myVisitRecord, potentialAppointmentInfo);
		this.potentialCustomerService.save(potentialCustomer);
	}

	@Transactional(readOnly = false)
	public void visitRecordDone(PotentialCustomer potentialCustomer, VisitRecord visitRecord,
								PotentialAfterSalesService potentialAfterSalesService)
			throws InvocationTargetException, IllegalAccessException {
		visitRecordService.createVisitRecord(potentialCustomer, visitRecord);//创建回访记录
		potentialCustomer.setDistStatus(DistEnum.UNDISTED.getValue());//清除分配信息
		potentialCustomer.setDistUserId(null);
		potentialCustomer.setExecutorId(null);//
		potentialCustomer.setExecutorName(null);
		potentialCustomer.clearGroup();//清除用户分组
		PotentialAdminGroup adminGroup = this.potentialAdminGroupService.getByName("成单数据");
		if (adminGroup != null) {
			potentialCustomer.setAdminGroup(adminGroup.getId(), null, null);
		}
		potentialCustomer.setWeight(100);// 成单后的权重改为100

		this.potentialCustomerService.save(potentialCustomer);
		List<PotentialCustomer> potentialCustomerList = new ArrayList<PotentialCustomer>();
		potentialCustomerList.add(potentialCustomer);
		// 个人数据按照回收逻辑合并到主数据
		importService.recover(potentialCustomerList);

		potentialAfterSalesService.setPotentialCustomerId(potentialCustomer.getId());
		this.potentialAfterSalesServiceService.save(potentialAfterSalesService);
	}

	@Transactional(readOnly = false)
	public void visitRecordEnd(PotentialCustomer potentialCustomer, VisitRecord visitRecord) {

		visitRecordService.createVisitRecord(potentialCustomer, visitRecord);
		if("public".equals(potentialCustomer.getRecordVisibilityRule())) {
			potentialCustomer.setDistStatus(DistEnum.UNDISTED.getValue());// 清除分配信息
			potentialCustomer.setDistUserId(null);
			potentialCustomer.setExecutorId(null);
			potentialCustomer.setExecutorName(null);
			potentialCustomer.clearGroup();//清除用户分组

		}
		this.potentialCustomerService.save(potentialCustomer);

		//TODO 是否删除掉各级数据状态 add by jief 20170215
		PotentialCustomerStatus status= new PotentialCustomerStatus();
		status.setFirsthandId(potentialCustomer.getId());
		this.potentialCustomerStatusDao.deleteByFirstHandId(status);
//		if (potentialCustomer.getLastCustomerStatusId() == null) {// 上次客户无状态，放入A类数据
//			PotentialAdminGroup adminGroup = this.potentialAdminGroupService.get("A类数据");
//			if (adminGroup != null) {
//				potentialCustomer.setAdminGroup(adminGroup.getId(), null, null);
//			}
//		} else if (potentialCustomer.getLastCustomerStatusId() != null) {// 上次客户状态为已发资料或者意向客户，放入B类数据
//			CustomerStatus customerStatus = customerStatusService.get(potentialCustomer.getLastCustomerStatusId());
//			if ("已发资料".equals(customerStatus.getContent()) || "意向客户".equals(customerStatus.getContent())) {
//				PotentialAdminGroup adminGroup = this.potentialAdminGroupService.get("B类数据");
//				if (adminGroup != null) {
//					potentialCustomer.setAdminGroup(adminGroup.getId(), null, null);
//				}
//			}
//		}

	}

}
